<template>
  <div>
    <div class="toolbar">
      <el-form :inline="true" :model="q" size="mini">
        <el-form-item label="渠道号:">
          <el-input v-model="q.name" prefix-icon="el-icon-edit"></el-input>
        </el-form-item>
        <el-form-item label="关联组员:">
          <el-select v-model="q.superior" filterable placeholder="请选择">
            <el-option v-for="item in  stafflist" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="关联组长:">
          <el-select v-model="q.leader_id" filterable placeholder="请选择">
            <el-option v-for="item in  leaderlist" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
          <el-button type="danger" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="listData.items" v-loading.body="listLoading" @sort-change="sortlist" size="mini" stripe border fit highlight-current-row>
      <el-table-column prop="name" label="渠道号"></el-table-column>
      <el-table-column prop="staff" label="关联组员"></el-table-column>
      <el-table-column prop="superior" label="关联上级"></el-table-column>
      <el-table-column prop="created_at" label="创建时间"></el-table-column>
      <el-table-column prop="description" label="备注"></el-table-column>
      <el-table-column label="操作" width="150" >
        <template slot-scope="scope">
          <div>
            <el-button type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="remove(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination  @size-change="handleSizeChange" @current-change="handleCurrentChange"
                      :current-page="q.page_no" :page-sizes="[10, 30, 100, 200, 500, 1000]"
                      :page-size="q.page_size"
                      layout="total, sizes, prev, pager, next, jumper"
                      :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-if="showUpdate" :showUpdate.sync="showUpdate" :Detail="Detail" :stafflist="stafflist"></save-update>
  </div>
</template>

<script>
  import saveUpdate from './saveUpdate'
  import { getchnList, deletechn, getChnOptions } from '@/api'
  import {queryFilter} from '@/utils/patch'
  export default {
    components: {saveUpdate},
    data() {
      return {
        listData: {},
        q: {page_size: 30, page_no: 1},
        listLoading: false,
        showUpdate: false,
        show_id: null,
        stafflist: [],
        leaderlist: [],
        staffArray: {},
        leaderArray: {},
        Detail: {},
      }
    },
    mounted(){
      this.getList()
    },
    created(){
      this.getStaffList()
      this.getLeaderList()
    },
    deactivated(){
      this.listLoading = false
    },
    methods: {
      add(){
        this.show_id = null
        this.showUpdate = true
      },
      update(row){
        this.Detail = row
        this.showUpdate = true
      },
      remove(row){
        this.$confirm(`确认删除渠道号为${row.name}的项么？`, '删除渠道', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deletechn(row.id).then(res => {
            if(res.status === 0){
              this.$message({ type: 'success', message: '删除成功!' })
              this.getList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      query(){
        this.q = Object.assign(this.q, {page_no: 1, page_size: 30})
        this.getList()
      },
      reload() {
        this.q = {page_size: 30, page_no: 1}
        this.getList()
      },
      sortlist(sort){
        this.q.$orderby = sort.prop
        this.getList()
      },
      handleSizeChange (val) {
        this.q.page_size = val
        this.getList()
      },
      handleCurrentChange (val) {
        this.q.page_no = val
        this.getList()
      },
      getStaffList(){
        getChnOptions({data_type: 'staff'}).then(res => {
          if(res.status === 0){
            this.stafflist = res.data
            this.stafflist.map(item => {
              this.staffArray[item.id] = item.name
            })
          }
        })
      },
      getLeaderList(){
        getChnOptions({data_type: 'leader'}).then(res => {
          if(res.status === 0){
            this.leaderlist = res.data
            this.leaderlist.map(item => {
              this.leaderArray[item.id] = item.name
            })
          }
        })
      },
      getList(){
        let queryObj = queryFilter(this.q)
        getchnList(queryObj).then(res => {
          if(res.status === 0){
            this.listLoading = false
            this.listData = res.data
          }
        })
      }
    },
    watch: {
      showUpdate() {
        if(!this.showUpdate){
          this.getList()
        }
      }
    }
  }
</script>
